package main

import "os"
import "fmt"
import "bufio"
import "strings"
import "gocalc/calcops"

func eval (input string) string {
	result, err := calcops.Evaluate (input)
	if err != nil {
		return err.Error()
	}
	return fmt.Sprintf ("%G", result)
}

func interpret () {
	reader := bufio.NewReader (os.Stdin)
	input  := ""
	for {
		fmt.Print (" > ") // print the prompt
		input,_ = reader.ReadString ('\n') // read the string
		// check to see if the input is "quit"
		if strings.ToLower (input) == "quit\n" { //HACK (the '\n' at the end of 'quit')
			break
		} else { // evaluate input
			fmt.Println ("   " + eval (input))
		}
	}
}

func main () {
	switch len (os.Args) {
		case 1: // 0 args
			interpret ()
		case 2: // 1 arg
			fmt.Println (eval (os.Args[1]))
		default:
			fmt.Println ("A single argument is expected.")
			fmt.Println ("Example usage: calc 1+2")
	}
}
